/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Threading;

namespace Xceed.Silverlight.Data
{
  internal class CustomAsyncResult : AsyncResultBase, IDisposable
  {
    internal CustomAsyncResult( Guid callId, object asyncState )
      : base( callId )
    {
      m_asyncState = asyncState;
    }

    public override object AsyncState
    {
      get
      {
        return m_asyncState;
      }
    }

    public override WaitHandle AsyncWaitHandle
    {
      get
      {
        if( m_waitHandle == null )
        {
          m_waitHandle = new ManualResetEvent( this.IsCompleted );
        }
        return m_waitHandle;
      }
    }

    public override bool CompletedSynchronously
    {
      get
      {
        return m_completedSynchronously;
      }
    }

    public override bool IsCompleted
    {
      get
      {
        return m_isCompleted;
      }
    }

    public virtual void Complete( bool synchronously, bool setWaitHandle )
    {
      m_isCompleted = true;
      m_completedSynchronously = synchronously;
      if( setWaitHandle )
      {
        this.SetWaitHandle();
      }
    }

    internal void SetWaitHandle()
    {
      if( m_waitHandle != null )
      {
        m_waitHandle.Set();
      }
    }


    #region IDisposable Members

    public void Dispose()
    {
      this.Dispose( true );
      GC.SuppressFinalize( this );
    }

    private bool m_disposed;//false;

    protected virtual void Dispose( bool disposing )
    {
      if( !m_disposed )
      {
        if( disposing )
        {
          if( m_waitHandle != null )
          {
            ( ( IDisposable )m_waitHandle ).Dispose();
          }
        }
        m_disposed = true;
      }
    }

    #endregion IDisposable Members


    #region Private Fields

    private object m_asyncState; // null;
    private bool m_completedSynchronously; // false;
    private bool m_isCompleted; // false;
    private ManualResetEvent m_waitHandle; //null;

    #endregion Private Fields
  }
}
